import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CacheModule } from '@nestjs/cache-manager';
import * as redisStore from 'cache-manager-redis-store';
import { APP_GUARD, Reflector } from '@nestjs/core';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { JwtService } from '@nestjs/jwt';

import { AuthModule } from './modules/people/auth/auth.module';
import { FriendModule } from './modules/people/friend/friend.module';
import { GroupModule } from './modules/people/group/group.module';
import { TransferModule } from './modules/file/transfer/transfer.module';
import { BasicModule } from './modules/file/basic/basic.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, //使ConfigModule配置会变得全局可用
      envFilePath: '.env',
    }),
    CacheModule.register({
      store: redisStore,
      host: 'localhost', // Redis 服务器地址
      port: 6379, // Redis 端口
      ttl: 300, // 默认过期时间(秒)
      isGlobal: true, // 全局模块
    }),
    AuthModule,
    FriendModule,
    GroupModule,
    TransferModule,
    BasicModule,
  ],
  controllers: [],
  providers: [
    {
      provide: APP_GUARD,
      useFactory: (reflector: Reflector, jwtService: JwtService) => {
        return new JwtAuthGuard(reflector, jwtService);
      },
      inject: [Reflector, JwtService], // 注入依赖
    },
    JwtService,
  ],
})
export class AppModule {}
